package com.dsteiner.xelavos.client.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import com.dsteiner.widgets.client.Widgets.IButton;
import com.dsteiner.widgets.client.Widgets.IHTML;
import com.dsteiner.widgets.client.Widgets.ILazyWidget;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.WidgetListenerAdapter;
import com.dsteiner.widgets.client.controller.PopupOverlayController;
import com.dsteiner.widgets.client.event.Event;
import com.dsteiner.widgets.client.event.EventListener;
import com.dsteiner.widgets.client.event.EventManager;
import com.dsteiner.widgets.client.event.EventManagerImpl;
import com.dsteiner.widgets.client.model.PopupOverlayModel;
import com.dsteiner.xelavos.client.ConfirmPopupWidget.ConfirmPopupModel;
import com.dsteiner.xelavos.client.event.Confirmable;
import com.dsteiner.xelavos.client.event.DeleteObjectEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;

public class ConfirmPopupController {

	private ConfirmPopupModel confirmPopupModel;
	private PopupOverlayModel popupOverlayModel;
	private IHTML messageHtml;
	private Confirmable lastConfirmableEvent;


	public void registerConfirmPopupWidget(ILazyWidget confirmPopupWidget) {
		confirmPopupWidget.addWidgetListener(new WidgetListenerAdapter() {
			@Override
			public void onAttach(IWidget widget) {
				updateMessage();
			}
		});
	}

	public void registerConfirmPopupModel(ConfirmPopupModel confirmPopupModel) {
		this.confirmPopupModel = confirmPopupModel;
		confirmPopupModel.addPropertyChangeListener("message",
				new PropertyChangeListener() {
					public void propertyChange(PropertyChangeEvent evt) {
						updateMessage();
					}
				});
	}

	private void updateMessage() {
		String message = confirmPopupModel.getMessage();
		if(messageHtml != null)
			messageHtml.setHTML(message);
	}

	public void registerEventManager(EventManager eventManager) {
		eventManager.registerListener(DeleteObjectEvent.class, new EventListener<DeleteObjectEvent>(){
			public void onEvent(DeleteObjectEvent event) {
				ConfirmPopupController.this.lastConfirmableEvent = event;
				if(!event.isConfirmed()){
					confirmPopupModel.setMessage("Realmente quiere borrar "+event.getModelObject()+" ?");
					PopupOverlayController.showAtCenter(popupOverlayModel);
				}
			}
		});
	}

	public void registerPopupOverlayModel(PopupOverlayModel popupOverlayModel) {
		this.popupOverlayModel = popupOverlayModel;
	}

	public void registerMessageHtml(IHTML messageHtml) {
		this.messageHtml = messageHtml;
	}

	public void registerCancelButton(IButton cancelButton) {
		cancelButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				popupOverlayModel.setVisible(false);
				lastConfirmableEvent.setConfirmed(false);
			}
		});
	}

	public void registerOkButton(IButton okButton) {
		okButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				lastConfirmableEvent.setConfirmed(true);
				EventManagerImpl.getInstance().fireEvent((Event) lastConfirmableEvent);
				popupOverlayModel.setVisible(false);
			}
		});
	}
}